package com.utils;

import java.io.File;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;

public class CosUtils {
	/**
	 * 建立cos客户端链接
	 * @return
	 */
	public static COSClient getCosClient() {
		// 1 初始化用户身份信息(secretId, secretKey)
		COSCredentials cred = new BasicCOSCredentials("AKIDqbDnDUXqriPwMdxnjdOzTny5A8ziQo7F", "GONEzhJpGlNp8KOPFvRnRwJWltsoiS3T");
		// 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
		// clientConfig中包含了设置region, https(默认http), 超时, 代理等set方法, 使用可参见源码或者接口文档FAQ中说明
		ClientConfig clientConfig = new ClientConfig(new Region("ap-chengdu"));
		// 3 生成cos客户端
		COSClient cosClient = new COSClient(cred, clientConfig);
		// bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
		String bucketName = "videos-1257508362";
		return cosClient;
	}
	/**
	 * 关闭cos客户端链接
	 * @param cosClient
	 */
	public static void clockCosClient(COSClient cosClient) {
		cosClient.shutdown();
	}
	public static PutObjectResult updateToCos(String bucketName,String fileName,File file, COSClient cosClient) {
		// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
		// 指定要上传到 COS 上对象键
		// 对象键（Key）是对象在存储桶中的唯一标识。例如，在对象的访问域名 `bucket1-1250000000.cos.ap-guangzhou.myqcloud.com/doc1/pic1.jpg` 中，对象键为 doc1/pic1.jpg, 详情参考 [对象键](https://cloud.tencent.com/document/product/436/13324)
		PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, file);
		PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
		return putObjectResult;
		
	}
}
